package com.example.axon.config;

import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.messaging.InterceptorChain;
import org.axonframework.messaging.MessageHandlerInterceptor;
import org.axonframework.messaging.unitofwork.UnitOfWork;

import java.util.Arrays;

@Slf4j
public class MyCommandHandlerInterceptor implements MessageHandlerInterceptor<CommandMessage<?>> {

    @Override
    public Object handle(UnitOfWork<? extends CommandMessage<?>> unitOfWork, InterceptorChain interceptorChain) throws Exception {
        CommandMessage<?> command = unitOfWork.getMessage();
//        String userId = Optional.ofNullable(command.getMetaData().get("userId"))
//                                .map(uId -> (String) uId)
//                                .orElseThrow(RuntimeException::new);
//        if ("axonUser".equals(userId)) {
//            return interceptorChain.proceed();
//        }
        Object payload = unitOfWork.getMessage().getPayload();
        log.info("interceptor log payload:: {}", payload);
        Object proceed = interceptorChain.proceed();
        log.info("interceptor log proceed:: {}", proceed);

        //            Arrays.stream(Thread.currentThread().getStackTrace()).forEach(o -> System.out.println("==============" + o.getMethodName()));
        return null;
    }
}